package controller;


import pojo.Book;
import pojo.Manage;
import pojo.User;
import service.BookService;
import service.ManageService;
import service.UserService;
import view.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class MainListener implements ActionListener {
    private MainJFrame frame;
    private JPanel panel;
    private CardLayout layout;
    private List<Book> bookList;
    private  List<User> userList;
    private List<Manage> manageList;
    private User user;

    public MainListener(MainJFrame frame,List<Book> bookList,List<User> userList) {
        this.frame = frame;
        this.bookList = bookList;
        this.userList=userList;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("图书分类")) {
            BookService bookService = new BookService();
            bookList = bookService.searchAll();
            BookClassification bookClassification = new BookClassification(bookList,frame.getUser());
            JPanel panel = frame.getPanel();
            CardLayout layout = frame.getLayout();
            panel.add(bookClassification, "class");
            layout.show(panel, "class");
        }


        if (e.getActionCommand().equals("用户管理")) {
            if(frame.getUser().getType().equals("系统管理员")) {
                UserService userService = new UserService();
                userList = userService.searchAll();
                UserView userView=new UserView(userList,frame.getUser());
                JPanel panel = frame.getPanel();
                CardLayout layout = frame.getLayout();
                panel.add(userView, "user1");
                layout.show(panel, "user1");
            }
            else{
                UserService userService=new UserService();
                User user =  userService.search(frame.getUser().getUserId());
                SelfUserView selfUserView =new SelfUserView(user);
                panel = frame.getPanel();
                CardLayout layout = frame.getLayout();
                panel.add(selfUserView, "user2");
                layout.show(panel, "user2");

            }
        }


        if (e.getActionCommand().equals("图书查询")) {
            if(frame.getUser().getType().equals("图书管理员")) {
                BookService bookService = new BookService();
                bookList = bookService.searchAll();
                QueryView queryView = new QueryView(bookList, frame.getUser());
                JPanel panel = frame.getPanel();
                CardLayout layout = frame.getLayout();
                panel.add(queryView, "book1");
                layout.show(panel, "book1");
            }
            else {
                BookService bookService = new BookService();
                bookList = bookService.searchAll();
                SelfQueryView selfQueryView = new SelfQueryView(bookList, frame.getUser());
                JPanel panel = frame.getPanel();
                CardLayout layout = frame.getLayout();
                panel.add(selfQueryView, "book2");
                layout.show(panel, "book2");
            }

        }
        if (e.getActionCommand().equals("租阅查询")) {
            if(frame.getUser().getType().equals("图书管理员")) {
                ManageService manageService = new ManageService();
                manageList = manageService.searchAll();
                ManageView manageView=new ManageView(manageList);
                JPanel panel = frame.getPanel();
                CardLayout layout = frame.getLayout();
                panel.add(manageView, "manage1");
                layout.show(panel, "manage1");
            }
            else {
                ManageService manageService = new ManageService();
                manageList = manageService.searchAll();
                SelfManageView selfManageView = new SelfManageView(manageList, frame.getUserId());
                JPanel panel = frame.getPanel();
                CardLayout layout = frame.getLayout();
                panel.add(selfManageView, "manage2");
                layout.show(panel, "manage2");
            }
        }
    }
}
